package com.monkeygroup.mygoplus.ecchat.ui.chatting.model;

import android.content.Context;
import android.text.TextUtils;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;

import com.monkeygroup.mygoplus.MyGoApplication;
import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.ecchat.ui.chatting.ChattingActivity;
import com.monkeygroup.mygoplus.ecchat.ui.chatting.holder.BaseHolder;
import com.monkeygroup.mygoplus.ecchat.ui.chatting.holder.SharedViewHolder;
import com.monkeygroup.mygoplus.ecchat.ui.chatting.view.CCPChattingFooter2;
import com.monkeygroup.mygoplus.ecchat.ui.chatting.view.ChattingItemContainer;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.yuntongxun.ecsdk.ECMessage;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2017/6/3.
 */
public class SharedTextTxRow extends BaseChattingRow {

    public SharedTextTxRow(int type) {
        super(type);
    }

    /* (non-Javadoc)
     * @see com.hisun.cas.model.im.ChattingRow#buildChatView(android.view.LayoutInflater, android.view.View)
     */
    @Override
    public View buildChatView(LayoutInflater inflater, View convertView) {
        //we have a don't have a converView so we'll have to create a new one
        if (convertView == null || ((BaseHolder) convertView.getTag()).getType() != mRowType) {

            convertView = new ChattingItemContainer(inflater, R.layout.row_chat_send_share);

            //use the view holder pattern to save of already looked up subviews
            SharedViewHolder holder = new SharedViewHolder(mRowType);
            convertView.setTag(holder.initBaseHolder(convertView, false));
        }
        return convertView;
    }

    @Override
    public void buildChattingData(Context context, BaseHolder baseHolder,
                                  ECMessage msg, int position) {
        SharedViewHolder holder = (SharedViewHolder) baseHolder;
        if (msg != null) {
            String head_url = "", nickname = "";
            if (!TextUtils.isEmpty(msg.getUserData())) try {
                JSONObject jsonObject = new JSONObject(msg.getUserData());
                JSONObject jsondata = jsonObject.getJSONObject(CCPChattingFooter2.SHARED_TEXT);
                head_url = jsondata.getString("head");
                nickname = jsondata.getString("nickname");
            } catch (JSONException e) {
                e.printStackTrace();
            }

            ImageLoader.getInstance().displayImage(head_url, holder.friend_head, MyGoApplication.getDefaultDisplayImgOption());
            holder.friend_nickname.setText(nickname);

            View.OnClickListener onClickListener = ((ChattingActivity) context).mChattingFragment.getChattingAdapter().getOnClickListener();
            ViewHolderTag holderTag = ViewHolderTag.createTag(msg,
                    ViewHolderTag.TagType.TAG_IM_TEXT, position);
            holder.getSharedContent().setTag(holderTag);
            holder.getSharedContent().setOnClickListener(onClickListener);

            getMsgStateResId(position, holder, msg, onClickListener);
        }
    }


    @Override
    public int getChatViewType() {
        return ChattingRowType.SHARED_ROW_TO.ordinal();
    }

    @Override
    public boolean onCreateRowContextMenu(ContextMenu contextMenu,
                                          View targetView, ECMessage detail) {

        return false;
    }


}